package org.jeecg.modules.aspect;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.Aspect;
import org.jeecg.common.aspect.annotation.ApiLogin;
import org.jeecg.common.exception.JeecgBoot401Exception;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.power.user.entity.YlToken;
import org.jeecg.modules.power.user.entity.YlUser;
import org.jeecg.modules.power.user.mapper.YlTokenMapper;
import org.jeecg.modules.power.user.mapper.YlUserMapper;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @BelongsProject: jeecg-boot-parent
 * @BelongsPackage: org.jeecg.modules.aspect
 * @Author: mi
 * @CreateTime: 2024-07-12 13:49
 * @Description:
 */
@Aspect
@Component
@Slf4j
public class ApiLoginAspct implements HandlerInterceptor {



    public static final String USER_INFO = "user";//用户对象

    public static final String SYS_USER = "sysUser";//用户对象


    @Autowired
    private YlUserMapper ylUserMapper;//会员用户表


    @Autowired
    private YlTokenMapper ylTokenMapper;//token表

    @Autowired
    private SysUserMapper userMapper;//运营端

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) {
        ApiLogin annotation;
        if(object instanceof HandlerMethod) {
            annotation = ((HandlerMethod) object).getMethodAnnotation(ApiLogin.class);
        }else{
            return true;
        }
        if(annotation == null){
            return true;
        }
        String errMsg="登陆已超时！请重新登陆";
        String token = request.getHeader("token"); // 从 http 请求头中取出 token
        if(StringUtils.isBlank(token)){
            throw new JeecgBoot401Exception("请登录!");
        }
        //设置userId到request里，后续根据userId，获取用户信息
        YlToken ylToken=ylTokenMapper.getToken(token);
        if (oConvertUtils.isEmpty(ylToken)){
            throw new JeecgBoot401Exception("你的账号在其他设备登陆!");//自定义异常处理
        }
        //判定 有没有超时
        if(  ylToken.getExpireTime().getTime()<System.currentTimeMillis()){
            ylTokenMapper.delGasToken(token);//清除过期token
            throw new JeecgBoot401Exception(errMsg);//自定义异常处理
        }
        if (ylToken.getType()==1){
            //运营
            SysUser user= userMapper.getUserPowerBankApp(ylToken.getUserId());
            if (oConvertUtils.isEmpty(user)){
                throw new JeecgBoot401Exception(errMsg);//自定义异常处理
            }
            if (user.getStatus()==2){// 状态(1：正常  2：冻结 ）
                throw new JeecgBoot401Exception("账号已被冻结！请联系管理员");//自定义异常处理
            }
            request.setAttribute(SYS_USER, user);
        }else {
            YlUser user=ylUserMapper.selectById(ylToken.getUserId());
            if (oConvertUtils.isEmpty(user)){
                throw new JeecgBoot401Exception(errMsg);//自定义异常处理
            }

            if (user.getStatus()==2){
                throw new JeecgBoot401Exception("账号已被冻结！请联系管理员");//自定义异常处理
            }
            request.setAttribute(USER_INFO, user);
        }
        return true;

    }
}
